﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Configuration; // 添加此引用
using DevExpress.XtraEditors; // 添加DevExpress控件引用

namespace BW.Mes.WinForms.Production
{
    public partial class UserControl1 : UserControl
    {
        #region 委托和事件

        /// <summary>
        /// 点击分页按钮的委托和事件
        /// </summary>
        /// <param name="currentPage">当前页</param>
        public delegate void ClickPageButtonHandler(int currentPage);
        public event ClickPageButtonHandler ClickPageButtonEvent;

        /// <summary>
        /// 改变每页显示条数的委托和事件
        /// </summary>
        public delegate void ChangedPageSizeHandler();
        public event ChangedPageSizeHandler ChangedPageSizeEvent;

        /// <summary>
        /// 跳转页面的委托和事件
        /// </summary>
        /// <param name="jumpPage">跳转的页码</param>
        public delegate void JumpPageHandler(int jumpPage);
        public event JumpPageHandler JumpPageEvent;

        #endregion

        #region 属性

        private int _currentPage = 1;
        /// <summary>
        /// 当前页码
        /// </summary>
        public int CurrentPage
        {
            get { return _currentPage; }
            set { _currentPage = value; }
        }

        private int _pageSize = 10;
        /// <summary>
        /// 每页显示记录数
        /// </summary>
        public int PageSize
        {
            get { return _pageSize; }
            set { _pageSize = value; }
        }

        private int _totalRows = 0;
        /// <summary>
        /// 总记录数
        /// </summary>
        public int TotalRows
        {
            get { return _totalRows; }
            set
            {
                _totalRows = value;
                lblTotalRows.Text = value.ToString();
            }
        }

        private int _totalPages = 1;
        /// <summary>
        /// 总页数
        /// </summary>
        public int TotalPages
        {
            get { return _totalPages; }
            set { _totalPages = value; }
        }

        /// <summary>
        /// 页码信息标签
        /// </summary>
        public LabelControl PageInfo
        {
            get { return lblPageInfo; }
        }

        /// <summary>
        /// 每页显示条数下拉框
        /// </summary>
        public ComboBoxEdit CboPageSize
        {
            get { return cboPageSize; }
        }

        /// <summary>
        /// 跳转页码文本框
        /// </summary>
        public TextBox JumpPageCtrl
        {
            get { return txtJumpPage; }
        }

        #endregion

        #region 构造函数

        private string _cfgPageSize = ConfigurationManager.AppSettings["PageSize"] ?? "10,20,50,100";

        public UserControl1()
        {
            InitializeComponent();

            // 绑定事件
            btnFirst.Click += BtnPage_Click;
            btnPrev.Click += BtnPage_Click;
            btnNext.Click += BtnPage_Click;
            btnLast.Click += BtnPage_Click;
            btnJump.Click += BtnJump_Click;
            cboPageSize.SelectedIndexChanged += CboPageSize_SelectedIndexChanged;

            // 为SimpleButton设置Tag属性，用于识别按钮类型
            btnFirst.Tag = "F";
            btnPrev.Tag = "P";
            btnNext.Tag = "N";
            btnLast.Tag = "L";

            // 初始化下拉框
            InitCboCtrl();

            // 初始化页码信息
            lblPageInfo.Text = string.Format("第{0}/{1}页", _currentPage, _totalPages);
            lblTotalRows.Text = _totalRows.ToString();
        }

        #endregion

        #region 初始化控件

        /// <summary>
        /// 初始化每页显示条数下拉框
        /// </summary>
        private void InitCboCtrl()
        {
            cboPageSize.Properties.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.DisableTextEditor;

            if (!string.IsNullOrEmpty(_cfgPageSize))
            {
                string cfgPageSize = _cfgPageSize.Replace("，", ",");
                if (cfgPageSize.EndsWith(","))
                {
                    cfgPageSize = cfgPageSize.Remove(cfgPageSize.Length - 1);
                }
                string[] strPageSize = cfgPageSize.Split(new char[] { ',' });
                List<string> listPageSize = new List<string>();
                for (int x = 0; x < strPageSize.Length; x++)
                {
                    if (!listPageSize.Contains(strPageSize[x]) && !string.IsNullOrEmpty(strPageSize[x]))
                    {
                        listPageSize.Add(strPageSize[x]);
                    }
                }

                cboPageSize.Properties.Items.Clear();
                for (int i = 0; i < listPageSize.Count; i++)
                {
                    cboPageSize.Properties.Items.Add(listPageSize[i]);
                }
            }
            else
            {
                cboPageSize.Properties.Items.Clear();
                cboPageSize.Properties.Items.Add("10");
                cboPageSize.Properties.Items.Add("20");
                cboPageSize.Properties.Items.Add("50");
                cboPageSize.Properties.Items.Add("100");
            }

            cboPageSize.SelectedIndex = 0;
            if (cboPageSize.SelectedIndex >= 0)
            {
                _pageSize = Convert.ToInt32(cboPageSize.Text);
            }
        }

        #endregion

        #region 事件处理

        /// <summary>
        /// 分页按钮点击事件
        /// </summary>
        private void BtnPage_Click(object sender, EventArgs e)
        {
            SimpleButton btn = sender as SimpleButton;
            if (btn != null)
            {
                switch (btn.Tag.ToString())
                {
                    case "F": // 首页
                        _currentPage = 1;
                        break;
                    case "P": // 上一页
                        if (_currentPage > 1)
                            _currentPage--;
                        break;
                    case "N": // 下一页
                        if (_currentPage < _totalPages)
                            _currentPage++;
                        break;
                    case "L": // 末页
                        _currentPage = _totalPages;
                        break;
                }

                // 更新页码显示
                lblPageInfo.Text = string.Format("第{0}/{1}页", _currentPage, _totalPages);

                // 触发事件
                if (ClickPageButtonEvent != null)
                {
                    ClickPageButtonEvent(_currentPage);
                }
            }
        }

        /// <summary>
        /// 跳转页面按钮点击事件
        /// </summary>
        private void BtnJump_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(txtJumpPage.Text))
            {
                int jumpPage;
                if (int.TryParse(txtJumpPage.Text, out jumpPage))
                {
                    if (jumpPage > 0 && jumpPage <= _totalPages)
                    {
                        _currentPage = jumpPage;

                        // 更新页码显示
                        lblPageInfo.Text = string.Format("第{0}/{1}页", _currentPage, _totalPages);

                        if (JumpPageEvent != null)
                        {
                            JumpPageEvent(jumpPage);
                        }
                    }
                    else
                    {
                        MessageBox.Show($"请输入1-{_totalPages}之间的页码", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
                else
                {
                    MessageBox.Show("请输入有效的页码", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }

        /// <summary>
        /// 每页显示条数改变事件
        /// </summary>
        private void CboPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cboPageSize.SelectedIndex >= 0)
            {
                _pageSize = Convert.ToInt32(cboPageSize.Text);
                _currentPage = 1; // 改变每页显示条数后，回到第一页

                // 更新页码显示
                lblPageInfo.Text = string.Format("第{0}/{1}页", _currentPage, _totalPages);

                if (ChangedPageSizeEvent != null)
                {
                    ChangedPageSizeEvent();
                }
            }
        }

        #endregion
    }
}